package cn.edu.zjgsu.ito.service;

import java.util.ArrayList;
import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

public class TeaRegraceService {
	/**
	 * 教师补考成绩录入
	 * 杨昆鹏
	 * */
	

	//补考成绩页面初始化
	public List<Record> index(String tNo){
		List<Record> stuInf = Db.find("select * from teacher_timetable where flag=? and tNo=?","补考",tNo);
		//uuid唯一化
//		for(int i = 0;i < stuInf.size();i++){
//			String uuid = stuInf.get(i).getStr("uuid");
//			for(int j = 0;j < i;j++){
//				String uuids = stuInf.get(j).getStr("uuid");
//				if(uuid.equals(uuids)){
//					stuInf.remove(j);
//				}
//			}
//		}
//		for(int i = 0;i < stuInf.size();i++){
//			System.out.println(stuInf.get(i).getStr("uuid"));
//		}
		return stuInf;
	}
	
	//补考成绩导入页面
	public List<Record> reGraImport(String uuid){
		Record boole = Db.findFirst("select * from teacher_timetable where flag=? and uuid=?","补考",uuid);
		if(boole != null){
			List<Record> stuInf = Db.find("select * from student_grade where uuid=?",uuid);
			List<Record> stuRe = new ArrayList<Record>();//不及格
			List<Record> stuReturn = new ArrayList<Record>();
			System.out.println(stuInf.size());
			for(int i = 0;i < stuInf.size();i++){
				String grade = stuInf.get(i).getStr("grade");
				int gradeStatus = stuInf.get(i).getInt("gradeStatus");
				if(grade.equals("优秀")||grade.equals("良好")||grade.equals("中等")||grade.equals("及格")||grade.equals("不及格")){
					if(grade.equals("不及格") && gradeStatus == 1 ){
						stuRe.add(stuInf.get(i));
					}
				}else{
					if(Integer.valueOf(grade) < 60 && gradeStatus == 1){
						stuRe.add(stuInf.get(i));
					}
				}
			}
			//判断数据中的gradeStatus属性
			int gradeStatus = 0 ;
			String grade = "";
			for(int i = 0;i < stuRe.size();i++){
				List<Record> temp = Db.find("select * from student_grade where uuid=?",uuid);
				for(int j = 0;j < temp.size();j++){
					if(temp.get(j).getInt("gradeStatus") == 0){
						gradeStatus = 0;
						grade = temp.get(j).getStr("grade");
					}else if(temp.get(j).getInt("gradeStatus") == 2){
						gradeStatus = 2;
						grade = temp.get(j).getStr("grade");
					}
				}
			}
			
			
			for(int i = 0;i < stuRe.size();i++){
				String passportNo = stuRe.get(i).getStr("passportNo");
				Record stu = Db.findFirst("select * from student_info where passportNo=?",passportNo)
						.set("grade",grade).set("gradeStatus",gradeStatus);
				stuReturn.add(stu);
			}
			System.out.println(stuReturn.size());
			return stuReturn;
		}
		return null;
	}

	//提交成绩时判断生成补考课程
	public void produceRepGra(String term,String tNo,String courseNo){
		List<Record> stuGraIf = Db.find("select * from student_grade where termValue=? and tNo=? and courseNo=?",term,tNo,courseNo);
//		double jige = 60;
		for(int i = 0;i < stuGraIf.size();i++){
			String grade = stuGraIf.get(i).getStr("grade");
			
			System.out.println("====");
			if(grade.equals("优秀")||grade.equals("良好")||grade.equals("中等")||grade.equals("及格")||grade.equals("不及格")){
				if( !grade.equals("不及格") ){
					stuGraIf.remove(i);
					i--;
				}
			}else{
				if(Integer.valueOf(grade) >= 60){
					stuGraIf.remove(i);
					i--;
				}
			}
		}
		String uuids = stuGraIf.get(0).getStr("uuid");
		Record week = Db.findFirst("select * from teacher_timetable where flag=? and uuid=?","补考",uuids);
		if(stuGraIf.size() > 0){
			System.out.println("有不及格的人");
			int temp = 0;
			if(week == null){
				temp = 1;
			}
			if(temp == 1){
				System.out.println("创建补考成绩");
				//有需要补考的
				Record oldwork = Db.findFirst("select * from teacher_timetable where uuid=?",uuids);
				Record newwork = new Record();
				newwork.set("tNo",oldwork.getStr("tNo"))
						.set("termValue",oldwork.getStr("termValue")).set("courseNo",oldwork.getStr("courseNo"))
						.set("startWeek",oldwork.getStr("startWeek")).set("endWeek",oldwork.getStr("endWeek")).set("flag","补考")
						.set("courseName",oldwork.getStr("courseName") + "(补考)").set("uuid",oldwork.getStr("uuid"));
				Db.save("teacher_timetable", newwork);
			}
		}
	}
	
	//保存补考成绩
	public void saveRepGrade(List<Record> stuInf){
		for(int i = 0;i < stuInf.size();i++){
			Record stu = new Record();
			
			String grade = stuInf.get(i).getStr("grade");
			String term = stuInf.get(i).getStr("termValue");
			String courseNo = stuInf.get(i).getStr("courseNo");
			String passportNo = stuInf.get(i).getStr("passportNo");
			
			List<Record> stuOld = Db.find("select * from student_grade where termValue=? and courseNo=? and passportNo=?",term,courseNo,passportNo);
			for(int j = 0;j < stuOld.size();j++){
				int gradeStatus = stuOld.get(j).getInt("gradeStatus");
				System.out.println(gradeStatus+"             -------------");
				if(gradeStatus == 0){
					Db.delete("student_grade", stuOld.get(j));
					System.out.println(gradeStatus+"      delete       -------------");
				}
				String majorNo = stuOld.get(j).getStr("majorNo");
				String majorName = stuOld.get(j).getStr("majorName");
				String courseName = stuOld.get(j).getStr("courseName");
				String tNo = stuOld.get(j).getStr("tNo");
				String uuid = stuOld.get(j).getStr("uuid");
				
				stu.set("termValue",term).set("passportNo",passportNo).set("majorNo",majorNo).set("majorName",majorName)
					.set("courseNo",courseNo).set("courseName",courseName).set("tNo",tNo).set("uuid",uuid)
					.set("gradeStatus",0).set("grade",grade);
				
				Db.save("student_grade", stu);
			}
		}
		
	}
	

	
	//提交补考成绩
	public void subRepGrade(List<Record> stuInf){
		for(int i = 0;i < stuInf.size();i++){
			Record stu = new Record();
			
			String grade = stuInf.get(i).getStr("grade");
			String term = stuInf.get(i).getStr("termValue");
			String courseNo = stuInf.get(i).getStr("courseNo");
			String passportNo = stuInf.get(i).getStr("passportNo");
			
			List<Record> stuOld = Db.find("select * from student_grade where termValue=? and courseNo=? and passportNo=?",term,courseNo,passportNo);
			for(int j = 0;j < stuOld.size();j++){
				int gradeStatus = stuOld.get(j).getInt("gradeStatus");
				System.out.println(gradeStatus+"             -------------");
				if(gradeStatus == 0){
					Db.delete("student_grade", stuOld.get(j));
					System.out.println(gradeStatus+"      delete0       -------------");
				}
				if(gradeStatus == 2){
					Db.delete("student_grade", stuOld.get(j));
					System.out.println(gradeStatus+"      delete2       -------------");
				}
				String majorNo = stuOld.get(j).getStr("majorNo");
				String majorName = stuOld.get(j).getStr("majorName");
				String courseName = stuOld.get(j).getStr("courseName");
				String tNo = stuOld.get(j).getStr("tNo");
				String uuid = stuOld.get(j).getStr("uuid");
				
				stu.set("termValue",term).set("passportNo",passportNo).set("majorNo",majorNo).set("majorName",majorName)
					.set("courseNo",courseNo).set("courseName",courseName).set("tNo",tNo).set("uuid",uuid)
					.set("gradeStatus",2).set("grade",grade);
				
				Db.save("student_grade", stu);
			}
		}
	}
	
}
